/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settings;

import com.google.android.tv.settings.TvSettings;
import com.google.android.tv.settings.TvSettings.Secure;
import com.google.tv.preference.Preferences;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.backup.IBackupManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.provider.Settings;
import android.util.Log;

/**
 * Settings for controlling privacy and content safety.
 */
public class PrivacySafetySettings extends SettingsPreferenceFragment
        implements OnSharedPreferenceChangeListener {
    private static final String TAG = "PrivacySafetySettings";

    /** UI keys for preference widgets */
    private static final String CHECKIN_LOGGING_ENABLED_KEY = "checkin_logging_enabled";
    private static final String SAFE_SEARCH_LEVEL_KEY = "safe_search_level";
    private static final String SAFETY_CATEGORY_KEY = "safety_category";
    private static final String PERSONAL_CATEGORY_KEY = "personal_category";

    private Preference mCheckinLoggingEnabled;
    private ListPreference mSafeSearchLevel;


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        getActivity().setTitle(R.string.privacy_safety_settings_title);
        addPreferencesFromResource(R.xml.privacy_safety_settings);

        final PreferenceScreen screen = getPreferenceScreen();

        initUI();
    }

    private void initUI() {
        mCheckinLoggingEnabled = findPreference(CHECKIN_LOGGING_ENABLED_KEY);
        mSafeSearchLevel = (ListPreference) findPreference(SAFE_SEARCH_LEVEL_KEY);
    }

    @Override
    public void onResume() {
        super.onResume();
        final Activity activity = getActivity();
        if (Secure.isCheckinUsageLoggingEnabled(getContentResolver())) {
            mCheckinLoggingEnabled.setSummary(R.string.checkin_logging_enabled_summary_on);
        } else {
            mCheckinLoggingEnabled.setSummary(R.string.checkin_logging_enabled_summary_off);
        }
        mSafeSearchLevel.setValue(Preferences.getSafeSearchLevel(activity).name());
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }

    public void onSharedPreferenceChanged(SharedPreferences preferences,
            String key) {
        if (key.equals(SAFE_SEARCH_LEVEL_KEY)) {
            final String levelName = mSafeSearchLevel.getValue();
            Preferences.SafeSearchLevel levelEnum;
            try {
                levelEnum = Preferences.SafeSearchLevel.valueOf(levelName);
            } catch (IllegalArgumentException e) {
                levelEnum = Preferences.DEFAULT_SAFE_SEARCH_LEVEL;
            }
            Preferences.setSafeSearchLevel(getActivity(), levelEnum);
        }
    }
}
